package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.ShiroUser;
import cn.stylefeng.guns.modular.system.entity.DealerLevel;
import cn.stylefeng.guns.modular.system.entity.Level;
import cn.stylefeng.guns.modular.system.entity.Price;
import cn.stylefeng.guns.modular.system.entity.ShiWarehouse;
import cn.stylefeng.guns.modular.system.mapper.LevelMapper;
import cn.stylefeng.guns.modular.system.mapper.PriceMapper;
import cn.stylefeng.guns.modular.system.model.params.PriceParam;
import cn.stylefeng.guns.modular.system.model.params.paramsDTO.PriceDTO;
import cn.stylefeng.guns.modular.system.model.params.paramsDTO.PriceRecieveDTO;
import cn.stylefeng.guns.modular.system.service.DealerLevelService;
import cn.stylefeng.guns.modular.system.service.LevelService;
import cn.stylefeng.guns.modular.system.service.PriceService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


/**
 * 产品价格表控制器
 *
 * @author 小飞
 * @Date 2019-06-27 14:31:13
 */
@Controller
@RequestMapping("/price")
public class PriceController extends BaseController {

    private String PREFIX = "/price";

    @Autowired
    private PriceService priceService;
    @Autowired
    private LevelService levelService;

    @Resource
    private LevelMapper levelMapper;

    @Resource
    private PriceMapper priceMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private DealerLevelService dealerLevelService;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("")
    public String index() {


        return PREFIX + "/price.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/add")
    public String add(Model model) {
        //经销商登录名
        ShiroUser shiroUser =  ShiroKit.getUserNotNull();
        model.addAttribute("username",shiroUser.getName());

        //部门
        String deptName = deptService.getById(shiroUser.getDeptId()).getSimpleName();
        model.addAttribute("deptName",deptName);

       /* //经销商名称
        String dealerName = dealerLevelService.getOne(new QueryWrapper<DealerLevel>()
                .eq("sys_user_id",shiroUser.getId())).getName();
        model.addAttribute("dealerName",dealerName);*/

        return PREFIX + "/price_add.html"; }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        List<Level> levels = levelService.list();
        model.addAttribute("levels",levels);
        return PREFIX + "/price_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(String data) {
        System.out.println("?????????????"+data+"!!!!!!!!!!!!");
        //把json数组解析成集合
        List<Price> prices = JSON.parseArray(data,Price.class);
        if(prices==null){
            return ResponseData.success();
        }
        /*Long productOrderId=prices.get(0).getProductOrderId();*/
        for(int i=0;i<prices.size();i++){
            /*prices.get(i).setProductOrderId(productOrderId);*/
            priceMapper.addItem(prices.get(i));
            System.out.println(prices.get(i).toString());
        }
       /* System.out.println(prices.toString());*/

       /* this.priceService.add(priceParam);*/
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PriceParam priceParam) {
        this.priceService.update(priceParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PriceParam priceParam) {
        this.priceService.delete(priceParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PriceParam priceParam) {
        Price detail = this.priceService.getById(priceParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PriceParam priceParam) {
        return this.priceService.findPageBySpec(priceParam);
    }


    /**
     * 获得所以产品等级
     *
     * @author zj
     * @Date 2019-06-30
     */
    @ResponseBody
    @RequestMapping("/getLevel")
    public List<Level> getLevel(){
        return levelService.list();
    }


    @ResponseBody
    @RequestMapping("/submit")
    public  ResponseData submit(PriceDTO priceDTO){

        List<PriceRecieveDTO> priceRecieveDTOS = JSON.parseArray(priceDTO.getPurchaseCost(),PriceRecieveDTO.class);
        Long produtctId = priceDTO.getProductId();
        Price price = null;
        //遍历插入数据
        for (PriceRecieveDTO priceRecieveDTO: priceRecieveDTOS){
             price = new Price();
             price.setProductOrderId(produtctId);
             price.setLevelId(priceRecieveDTO.getWarhouseLevelId());
             price.setProductPrice(priceRecieveDTO.getCostPurchaseAmount());
             priceService.getBaseMapper().insert(price);
        }
        return  ResponseData.success();
    }
}


